<?php
namespace Admin\Controller;
use Think\Controller;
class UadminController extends CommonController {
	//添加页面
    public function add(){
        //创建模板
        $think_auth_group = M('think_auth_group');
        $ginfo = $think_auth_group->select();
        $this->assign('ginfo',$ginfo);
        // var_dump($ginfo);die;
    	$this->display();
    }
    // 判断ajax
    public function ajax(){
    	$res = $_POST['res'];
    	// echo $res;
    	//创建数据库
    	$admin = M('mvp_admin');
    	//创建数据
    	// $admin->create();
    	$num = $admin->where('ausername='."'".$res."'")->find();
    	// echo $Mclass->_sql();
    	// var_dump($num);
    	if(empty($num)){
    		echo 1;
    	}else{
    		echo 0;
    	}

    }
    //执行添加
    public function insert(){
    	// var_dump($_POST);
     //    die;
       
        
    	//创建数据库
    	$admin = M('mvp_admin');
        $think_auth_group_access = M('think_auth_group_access'); 
    	//创建数据
    	$admin->create();
    	//执行添加
    	$cid=$admin->add();
        if($cid){
            //插入权限和用户
            $_POST['uid'] = $cid;
            $think_auth_group_access -> create();
            $groupid = $think_auth_group_access -> add();
        }
    	if($groupid){
    		$this->success('添加成功',U('Admin/Uadmin/index'),3);
    	}else{
    		$this->success('添加失败',U('Admin/Uadmin/index'),3);

    	}

    }

    //分类列表页面
    public function index(){
    	//获取数据库
    	$admin = M('mvp_admin');
        $think_auth_group = M('think_auth_group');
    	//获取数据
    	$info = $admin->field('mvp_admin.*,think_auth_group_access.group_id')->join('think_auth_group_access on mvp_admin.id=think_auth_group_access.uid')->order('mvp_admin.id')->select();
        //echo $admin->_sql();die;
        foreach ($info as $key => $value) {
            $groupres = $think_auth_group->where('id = '.$value['group_id'])->select();
            $info[$key]['title'] = $groupres[0]['title'];
        }
        // var_dump($info);die;

    	$this->assign('info',$info);
    	// var_dump($info);
    	$this->display();
    }

    public function edit(){
    	//获取要修改的id
    	$id = I('get.id');
    	// echo $id;
    	//查询数据管理组
    	$admin = M('mvp_admin');
    	$info = $admin->where('mvp_admin.id='.$id)->select();
        //查询用户权限
        $think_auth_group_access = M('think_auth_group_access');
        $groupinfo = $think_auth_group_access->where('think_auth_group_access.uid = '.$id)->join('think_auth_group on think_auth_group_access.group_id = think_auth_group.id')->select();
        // echo $think_auth_group_access->_sql();
        //查询所有的权限
        $think_auth_group = M('think_auth_group');
        $rolesinfo = $think_auth_group->select();
        // var_dump($groupinfo);
    	// var_dump($rolesinfo);
    	// 分配
        $this->assign('info',$info);
        $this->assign('groupinfo',$groupinfo);
    	$this->assign('rolesinfo',$rolesinfo);
    	//展示要修改的类
    	$this->display();
    }

    public function update(){
    	// var_dump($_POST);
     //    die;
    	//查询数据
    	$admin = M('mvp_admin');
        //创建数据
        $admin->create();
        //执行修改
        $res = $admin->save();
        //echo $User->_sql();

        // var_dump($res);die;
        //更新权限组表
        $think_auth_group_access = M('think_auth_group_access');
        //var_dump($info);die;
        //创建
        $think_auth_group_access->create();
        //更新
        $res2 = $think_auth_group_access->where('uid = '.$_POST['id'])->save();
       // echo $info->_sql();
        //var_dump($res2);die;
        // echo $num;
        // echo $Mclass->_sql();die;
    	if($res || $res2){
    		$this->success('修改成功',U('Admin/Uadmin/index'),3);
    	}else{
    		$this->error('修改失败',U('Admin/Uadmin/index'),3);

    	}
    }



    public function delete(){
        //获取id
        $id = I('get.id');
        //创建模型
        $m = new \Think\Model();//查找该文件
        ///开启事物
        $m->startTrans();
        //创建对象
        $one = $m->table('mvp_admin')->where('id='.$id)->delete();
        $two = $m->table('think_auth_group_access')->where('uid = '.$id)->delete();
        //判断
        if($one && $two){
            $m->commit();
            $this->success('删除成功',U('Admin/Uadmin/index'),3);
        }else{
            $m->rollback();
            $this->error('删除失败',U('Admin/Uadmin/index'),3);
        }
    }


}